'use strict';

const Controller = require('egg').Controller;
const mysql = require("mysql");
const jwt = require("jsonwebtoken")
class HomeController extends Controller {
  //登录
  async login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('login', { username, password })
    const token = jwt.sign(username, 'lm')
    if (data) {
      ctx.body = {
        code: 1,
        msg: '登录成功',
        token,
        data
      }
    } else {
      ctx.body = {
        code: 0,
        msg: '登录失败'
      }
    }
  }
  //渲染 从后台那数据
  async getList() {
    const { ctx, app } = this;
    const data = await app.mysql.select('mocklist')

    ctx.body = {
      code: 200,
      data
    }
  }
  //消息列表
  async addlist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('messagelist')

    ctx.body = {
      code: 200,
      data
    }
  }
  //求租、求购列表
  async renting() {
    const { ctx, app } = this;
    const data = await app.mysql.select('rentinglist')

    ctx.body = {
      code: 200,
      data
    }
  }
  //筛选
  async screening3() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("mocklist", { where: { type: value } })

    if (value === '户型') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  };
  //筛选
  async screening4() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("mocklist", { where: { group: value } })

    if (value === '筛选') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }
  //筛选
  async screening1() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("mocklist", { where: { city: value } })

    if (value === '区域') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  //详情
  async detail() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('mocklist', { id })
    ctx.body = {
      code: 1,
      data
    }
  }
  //删除
  async delList() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    app.mysql.delete('messagelist', { id })

    ctx.body = {
      code: 200,
      msg: '删除成功'
    }
  }
  //求租、求购筛选
  async screening5() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("rentinglist", { where: { name: value } })

    if (value === '户型') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  };
  async screening6() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("rentinglist", { where: { title: value } })

    if (value === '筛选') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }
  //排行榜数据
  async getList() {
    const { ctx, app } = this;
    const data = await app.mysql.select('mocklist')
    ctx.body = {
      code: 200,
      data
    }
  }



  async paidetail() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('mocklist', { id })
    ctx.body = {
      code: 200,
      data
    }
  }
}
module.exports = HomeController;
